package com.lightsh.utility.testactivity;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.lightsh.email.EmailHelper;
import com.lightsh.provider.MyContentProvider;
import com.lightsh.sensor.SensorTester;
import com.lightsh.utility.R;
import com.lightsh.webview.JsWebView;
import com.lightsh.webview.SimpleWebView;
import com.ligthsh.db.MyDbHelper;

import android.R.color;
import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class MainActivity extends Activity{
	private SensorTester mSensorTester;
	private SimpleWebView mWebView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mWebView = (SimpleWebView)findViewById(R.id.jsWebView);
		
		//testUpdate("testUpdate1", 35, 100);
//		testContentObserver();
//		testInsert();
		//testDb();
//		testEmail();
//		mSensorTester = new SensorTester(this);
		mWebView.startTest();
	}
	
	private void testEmail(){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String date = format.format(new Date(System.currentTimeMillis()));
				EmailHelper.fetchEmail("imap", "imap.163.com", "shu_jun_feng@163.com", "***");
				
//				EmailHelper.sendEmail("smtp.163.com", "shu_jun_feng@163.com", "*****", "233950783@qq.com", "测试邮件发送", "邮件内容...\r\n发送日期: " + date, new String[]{"/sdcard/pic.jpg", "/sdcard/I Love You.mp3"});
				
			}
		}).start();
	}
	
	public void onDestroy(){
		if(mbRegisterObserver){
			mbRegisterObserver = false;
			getContentResolver().unregisterContentObserver(mObserver);
		}
		if(mSensorTester != null){
			mSensorTester.end();
		}
		super.onDestroy();
	}
	
	private void testDb(){
		MyDbHelper db = new MyDbHelper(this);
		db.insertData("a", "abc", 1);
		db.insertData("b", "abc", 2);
		db.insertData("c", "abc", 3);
		
		Cursor cursor = db.fetchAllData();
		Log.e("", MyDbHelper.toString(cursor));	
		cursor.close();
		db.close();
	}
	
	private void testUpdate(String text, int i, int j){
		ContentValues values = new ContentValues();
		values.put(MyDbHelper.FIELD_TEXT, text);
		
		showData();
		
		Log.e("update count : ", "" + getContentResolver().update(ContentUris.withAppendedId(MyContentProvider.CONTENT_URI, i), values, null, null));
		
		Log.e("update count : ", "" + getContentResolver().update(MyContentProvider.CONTENT_URI, values, MyDbHelper.FIELD_ID + "=" + j, null));
		
		showData();
	}
	
	private void showData(){
		Cursor cursor = getContentResolver().query(MyContentProvider.CONTENT_URI, null, null, null, null);
		Log.e("current data : ", MyDbHelper.toString(cursor));
		cursor.close();
	}
	
	private void testInsert(){
		ContentValues values = new ContentValues();
		values.put(MyDbHelper.FIELD_NAME, "a");
		values.put(MyDbHelper.FIELD_TEXT, "abc");
		values.put(MyDbHelper.FIELD_INT_VALUE, 1);
		getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
		
		values.put(MyDbHelper.FIELD_NAME, "b");
		values.put(MyDbHelper.FIELD_TEXT, "abc");
		values.put(MyDbHelper.FIELD_INT_VALUE, 2);
		getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
		
		values.put(MyDbHelper.FIELD_NAME, "c");
		values.put(MyDbHelper.FIELD_TEXT, "abc");
		values.put(MyDbHelper.FIELD_INT_VALUE, 3);
		getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
		
		showData();
		
		Cursor cursor = getContentResolver().query(MyContentProvider.CONTENT_URI, null, null, null, null);
		int count = 0;
		if(cursor.moveToFirst()){
			while(true){
				count ++;
				if(!cursor.moveToNext()){
					break;
				}
			}
		}
		cursor.close();
		if(count > 20){
			Log.e("delete count : ", "" + getContentResolver().delete(MyContentProvider.CONTENT_URI, null, null));
		}
	}
	
	private boolean mbRegisterObserver = false;
	private void testContentObserver(){
		getContentResolver().registerContentObserver(MyContentProvider.CONTENT_URI, true, mObserver);
		mbRegisterObserver = true;
	}
	private ContentObserver mObserver = new ContentObserver(new Handler()) {

		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);
			Log.e("ContentObserver", "onChange:" + selfChange);
		}
		
	};
	@Override
	public void onBackPressed() {
		if(!mWebView.handleBackPressed()){
			super.onBackPressed();
		}
	}
	
	
}
